package b1.scenes.entry
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	import b1.scenes.main.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;

	public class AnimalSelector extends Sprite
	{
		//背景框
//		private var selectorBackground:Sprite;
		//左箭头 
		private var leftArrow:BasicIcon;
		//右箭头
		private var rightArrow:BasicIcon;
		//动物列表
		private var animalList:Array;
		//当前显示的动物索引
		private var currentAnimalIndex:int;
		
		public function AnimalSelector(camp:int)
		{
			super();
			//动物列表
			animalList = new Array();
			var animalTypeStr:String;
			for (var i:int = 0; i < Config.animalConfigs.length; i++)
			{
				var animalConfig:AnimalConfig = Config.animalConfigs[i] as AnimalConfig;
				if (animalConfig.camp == camp)
				{
					var animalItem:AnimalItem = new AnimalItem(animalConfig);
					animalItem.x = 50;
					animalItem.y = 20;
					animalList.push(animalItem);
					addChild(animalItem);
				}
			}
			currentAnimalIndex = Math.random() * animalList.length;
			for (i = 0; i < animalList.length; i++)
			{
				if (i != currentAnimalIndex)
				{
					animalList[i].visible = false;
				}
			}
			//左箭头
			var LeftArrow:Class = ResourceHelper.getInstance().getClassByName("LeftArrowButton2");
			leftArrow = new BasicIcon(LeftArrow);
			leftArrow.addEventListener(MouseEvent.CLICK, onLeftArrowMouseClick);
			leftArrow.x = 10;
			leftArrow.y = 100;
			addChild(leftArrow);
			//右箭头
			var RightArrow:Class = ResourceHelper.getInstance().getClassByName("RightArrowButton2");
			rightArrow = new BasicIcon(RightArrow);
			rightArrow.addEventListener(MouseEvent.CLICK, onRightArrowMouseClick);
			rightArrow.x = 200;
			rightArrow.y = 100;
			addChild(rightArrow);
		}
		
		private function setShow(index:int):void
		{
			animalList[currentAnimalIndex].visible = false;
			animalList[index].visible = true;
			var fe:FirstEntry = SceneContainer.getInstance().currentScene as FirstEntry;
			fe.descriptionText.text = (animalList[index] as AnimalItem).animalConfig.description;
			fe.descriptionText.x = this.x + 30;
			fe.descriptionText.y = this.y + 170;
			currentAnimalIndex = index;
		}
		
		private function onLeftArrowMouseClick(event:MouseEvent):void
		{
			var index:int = (animalList.length + currentAnimalIndex - 1) % animalList.length; 
			setShow(index);
		}
		
		private function onRightArrowMouseClick(event:MouseEvent):void
		{
			var index:int = (currentAnimalIndex + 1) % animalList.length; 
			setShow(index);
		}
		
	}
}